package cn.csc.module.pay.dal.mysql.bank;

import java.math.BigInteger;
import java.util.*;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.module.pay.controller.admin.fund.vo.PayFundPageReqVO;
import cn.csc.module.pay.controller.app.jzb.vo.JzbDrawCashReqVO;
import cn.csc.module.pay.dal.dataobject.bank.PayBankDO;
import cn.csc.module.pay.dal.dataobject.fund.PayFundDO;
import cn.csc.module.pay.dal.dataobject.order.PayOrderDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.csc.module.pay.controller.admin.bank.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 资金账户绑定的银行卡 Mapper
 *
 * @author 华南城
 */
@Mapper
public interface PayBankMapper extends BaseMapperX<PayBankDO> {

    default PageResult<PayBankDO> selectPage(PayBankPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PayBankDO>()
                .eqIfPresent(PayBankDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(PayBankDO::getAppUserId, reqVO.getAppUserId())
                .likeIfPresent(PayBankDO::getMemberName, reqVO.getMemberName())
                .eqIfPresent(PayBankDO::getMemberGlobalType, reqVO.getMemberGlobalType())
                .eqIfPresent(PayBankDO::getMemberGlobalId, reqVO.getMemberGlobalId())
                .eqIfPresent(PayBankDO::getMemberAcctNo, reqVO.getMemberAcctNo())
                .eqIfPresent(PayBankDO::getBankType, reqVO.getBankType())
                .likeIfPresent(PayBankDO::getAcctOpenBranchName, reqVO.getAcctOpenBranchName())
                .eqIfPresent(PayBankDO::getCnapsBranchId, reqVO.getCnapsBranchId())
                .eqIfPresent(PayBankDO::getEiconBankBranchId, reqVO.getEiconBankBranchId())
                .eqIfPresent(PayBankDO::getMobile, reqVO.getMobile())
                .eqIfPresent(PayBankDO::getIndivBusinessFlag, reqVO.getIndivBusinessFlag())
                .likeIfPresent(PayBankDO::getCompanyName, reqVO.getCompanyName())
                .eqIfPresent(PayBankDO::getCompanyGlobalType, reqVO.getCompanyGlobalType())
                .eqIfPresent(PayBankDO::getCompanyGlobalId, reqVO.getCompanyGlobalId())
                .eqIfPresent(PayBankDO::getShopId, reqVO.getShopId())
                .likeIfPresent(PayBankDO::getShopName, reqVO.getShopName())
                .eqIfPresent(PayBankDO::getRepFlag, reqVO.getRepFlag())
                .likeIfPresent(PayBankDO::getReprName, reqVO.getReprName())
                .eqIfPresent(PayBankDO::getReprGlobalType, reqVO.getReprGlobalType())
                .eqIfPresent(PayBankDO::getReprGlobalId, reqVO.getReprGlobalId())
                .betweenIfPresent(PayBankDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(PayBankDO::getState, reqVO.getState())
                .orderByDesc(PayBankDO::getId));
    }

    default List<PayBankDO> selectList(PayBankExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<PayBankDO>()
                .eqIfPresent(PayBankDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(PayBankDO::getAppUserId, reqVO.getAppUserId())
                .likeIfPresent(PayBankDO::getMemberName, reqVO.getMemberName())
                .eqIfPresent(PayBankDO::getMemberGlobalType, reqVO.getMemberGlobalType())
                .eqIfPresent(PayBankDO::getMemberGlobalId, reqVO.getMemberGlobalId())
                .eqIfPresent(PayBankDO::getMemberAcctNo, reqVO.getMemberAcctNo())
                .eqIfPresent(PayBankDO::getBankType, reqVO.getBankType())
                .likeIfPresent(PayBankDO::getAcctOpenBranchName, reqVO.getAcctOpenBranchName())
                .eqIfPresent(PayBankDO::getCnapsBranchId, reqVO.getCnapsBranchId())
                .eqIfPresent(PayBankDO::getEiconBankBranchId, reqVO.getEiconBankBranchId())
                .eqIfPresent(PayBankDO::getMobile, reqVO.getMobile())
                .eqIfPresent(PayBankDO::getIndivBusinessFlag, reqVO.getIndivBusinessFlag())
                .likeIfPresent(PayBankDO::getCompanyName, reqVO.getCompanyName())
                .eqIfPresent(PayBankDO::getCompanyGlobalType, reqVO.getCompanyGlobalType())
                .eqIfPresent(PayBankDO::getCompanyGlobalId, reqVO.getCompanyGlobalId())
                .eqIfPresent(PayBankDO::getShopId, reqVO.getShopId())
                .likeIfPresent(PayBankDO::getShopName, reqVO.getShopName())
                .eqIfPresent(PayBankDO::getRepFlag, reqVO.getRepFlag())
                .likeIfPresent(PayBankDO::getReprName, reqVO.getReprName())
                .eqIfPresent(PayBankDO::getReprGlobalType, reqVO.getReprGlobalType())
                .eqIfPresent(PayBankDO::getReprGlobalId, reqVO.getReprGlobalId())
                .betweenIfPresent(PayBankDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(PayBankDO::getState, reqVO.getState())
                .orderByDesc(PayBankDO::getId));
    }
    default int updateState(PayBankDO payBankDO){
        return update(payBankDO, new UpdateWrapper<PayBankDO>()
                .eq("app_user_id", payBankDO.getAppUserId())
                .eq("merchant_id", payBankDO.getMerchantId()));
    }

    default int updateStateById(PayBankDO payBankDO){
        return update(payBankDO, new UpdateWrapper<PayBankDO>()
                .eq("member_acct_no", payBankDO.getMemberAcctNo())
                .eq("app_user_id", payBankDO.getAppUserId())
                .eq("merchant_id", payBankDO.getMerchantId()));
    }

    /**
     * 查询绑定成功的卡
     * @param reqVO
     * @return
     */
    default PayBankDO selectByAppUserId(JzbDrawCashReqVO reqVO){
        return selectOne(new LambdaQueryWrapperX<PayBankDO>()
                .eqIfPresent(PayBankDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(PayBankDO::getAppUserId, reqVO.getAppUserId())
                .eqIfPresent(PayBankDO::getState, 1)
        );
    }

    @Select({"<script>","select pb.id, pb.member_name ,pb.member_acct_no ,pb.bind_bank_type, pb.member_global_type, pb.member_global_id, " +
            " pf.jzb_code, pb.acct_open_branch_name, pb.indiv_business_flag, pb.bank_name, pb.mobile, pb.company_name, pb.shop_id, " +
            " pb.shop_name, pb.rep_flag, pb.repr_name, pb.repr_global_type, pb.repr_global_id, pb.company_global_type, pb.company_global_id, " +
            " pb.agency_client_flag, pb.agency_client_name, pb.agency_client_global_type, pb.agency_client_global_id, pb.agency_client_mobile " +
            "from pay_bank pb left join pay_fund pf on pb.merchant_id=pf.merchant_id  and pb.app_user_id = pf.app_user_id " +
            "where pb.app_user_id = #{appUserId} and pb.state = #{state} " +
            "<if test=\"merchantId != null and merchantId != ''\"> and pb.merchant_id = #{merchantId} </if>  ", "</script>"})
    PayBankRespApiVO selectByAppUserId(@Param("merchantId") BigInteger merchantId, @Param("appUserId") String appUserId, @Param("state") int state);

    default PayBankDO selectByAppUserId(BigInteger merchantId, String appUserId){
        return selectOne(new LambdaQueryWrapperX<PayBankDO>()
                .eq(PayBankDO::getMerchantId, merchantId)
                .eq(PayBankDO::getAppUserId, appUserId));
    }

}
